//
//  SettingController.m
//  mid
//
//  Created by itlab on 2020/12/4.
//  Copyright © 2020 itlab. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "SettingController.h"
#import "LoginVC.h"
#import "InfoVC.h"
#import "NotificationVC.h"

@interface SettingController()<infoReceiver,UITableViewDelegate,UITableViewDataSource>{
    InfoVC *infovc;
    NotificationVC *vc;
}
@end

@implementation SettingController
-(id) init{
    self = [super init];
    self.title = @"Setting";
    _option_list = [@[
                      @{@"name":@"个人信息",
                        @"list":@[@"个人信息",@"成就"]},
                      @{@"name":@"存储",
                        @"list":@[@"存储额度"]},
                      @{@"name":@"个性化",
                        @"list":@[@"夜间模式"]},
                      ] mutableCopy];
    return self;
}

-(void)viewDidLoad{
    [super viewDidLoad];
//    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
    _setting_view = [[SettingView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    [_setting_view viewInit];
    
    [_setting_view.loginButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    [_setting_view.message_button addTarget:self action:@selector(showNotification) forControlEvents:UIControlEventTouchUpInside];
    [_setting_view.style_switch addTarget:self action:@selector(styleChange) forControlEvents:UIControlEventTouchUpInside];
    
    [_setting_view.setting_table setDataSource:self];
    [_setting_view.setting_table setDelegate:self];
    [self.view addSubview:_setting_view];
}
#pragma mark Login
-(void)setInfo:(UserInfo*)info{
    NSLog(@"setinfo");
    _user_info = [info copy];
    [_setting_view showAvatar:nil];
    [[_setting_view setting_table] reloadData];
    [self.view reloadInputViews];
}
-(void)login{
//    NSLog(@"push");
    LoginVC *vc = [[LoginVC alloc]init];
    [vc setDelegate:self];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark Comment
-(void)showNotification{
    if (_user_info == nil) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你需要先登录" delegate:self cancelButtonTitle:@"我了解了" otherButtonTitles:nil];
        [alert show];
        return;
    }
    if (vc == nil) {
        vc = [[NotificationVC alloc]init];
        [vc viewDidLoad];
    }
    [vc loadMsgs];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark Style Switch
-(void)styleChange{
    // TODO 切换主题
}
#pragma mark UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView: (UITableView *)tableView{
    return _option_list.count;
}

-(NSInteger)tableView: (UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_option_list[section][@"list"] count];
}

-(NSString *)tableView: (UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return _option_list[section][@"name"];
}

-(UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    NSString *cellID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (nil == cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
    }
    cell.textLabel.text = _option_list[indexPath.section][@"list"][indexPath.row];
    if ([cell.textLabel.text isEqualToString:@"存储额度"]) {
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%zd/%zd",[_user_info UsedSize],[_user_info MaxSize]];
    } else if ([cell.textLabel.text isEqualToString:@"夜间模式"]){
        [cell.contentView addSubview:[_setting_view style_switch]];
    } else {
        cell.detailTextLabel.text = @">";
    }
    return cell;
}

-(CGFloat)tableView: (UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section == 0){
        double status_bar_height = [[UIApplication sharedApplication] statusBarFrame].size.height;
        return status_bar_height-15.0f; // it looks good somehow not knowing why
    }
    return 12.0f;
}

-(CGFloat)tableView: (UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    return 60.0f;
}

-(void)tableView: (UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
     [tableView deselectRowAtIndexPath:indexPath animated:NO];
    NSString *option = _option_list[indexPath.section][@"list"][indexPath.row];
    // 因为判断少，而且应该是不会继续扩展了，直接用分支比较方便
    if([option isEqualToString:@"个人信息"]){
        if (_user_info == nil) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你需要先登录" delegate:self cancelButtonTitle:@"我了解了" otherButtonTitles:nil];
            [alert show];
            return;
        }
        if (infovc == nil) {
            infovc = [[InfoVC alloc]init];
            [infovc viewDidLoad];
        }
        [infovc setInfo:_user_info];
        [self.navigationController pushViewController:infovc animated:YES];
    } else if ([option isEqualToString:@"成就"]){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"没有API，获取不到滴。。" delegate:self cancelButtonTitle:@"我了解了" otherButtonTitles:nil];
        [alert show];
        return;
    }
}

@end
